# -*- coding: utf-8 -*
import logging
import colorlog

from typing import List

LOG_LEVEL = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
LEVEL_MAP = {
    "DEBUG": logging.DEBUG,
    "INFO": logging.INFO,
    "WARNING": logging.WARNING,
    "ERROR": logging.ERROR,
    "CRITICAL": logging.CRITICAL
}


class Logger:
    """日志管理器"""
    def __init__(self, name: str, color: List[str] = ["white", "green", "yellow", "red", "bold_red"], level: str = "DEBUG"):
        self.logger = logging.getLogger(name)
        self.logger.setLevel(LEVEL_MAP[level])

        console = logging.StreamHandler()
        console.setLevel(LEVEL_MAP[level])

        self.config = {}
        for index, item in enumerate(LOG_LEVEL):
            self.config[item] = color[index]

        console_formatter = colorlog.ColoredFormatter(
            fmt='%(log_color)s[%(asctime)s] '
                '%(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
            datefmt="%Y-%m-%d %H:%M:%S",
            log_colors=self.config
        )
        console.setFormatter(console_formatter)
        self.logger.addHandler(console)

    def get_logger(self):
        """一般用于终端日志输出"""
        return self.logger
